package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.demon.DanWeiList;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * Created by Administrator on 2017/5/5.
 */

public class ChangeDanWeiActivity extends BaseActivity {
    @BindView(R.id.et_user_danwei)
    EditText etUserDanwei;
    @BindView(R.id.bt_dw_queding)
    Button btQueding;
    int IsNeedData = 2;
    @BindView(R.id.list_changedanwei)
    ListView listChangedanwei;
    @BindView(R.id.view_changebottom)
    View viewChangebottom;
    @BindView(R.id.li_changedanwei)
    LinearLayout liChangedanwei;
    @BindView(R.id.iv_change_dw)
    ImageView ivChangeDw;
    private String dw_id;//选中单位id
    ArrayList<DanWeiList.RowsBean> Temp_list = new ArrayList<>();
    private DWListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changedanwei);
        ButterKnife.bind(this);
        ChangLayTitle("单位");
        LayBack();
        init();
    }

    private void init() {
        etUserDanwei.setText(getIntent().getStringExtra("dw"));
        etUserDanwei.addTextChangedListener(this);
        if(!TextUtils.isEmpty(etUserDanwei.getText().toString())){
            btQueding.setClickable(true);
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            ivChangeDw.setVisibility(View.VISIBLE);
        }else {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
            ivChangeDw.setVisibility(View.GONE);
        }
        //单位自动补全
        adapter = new DWListAdapter(Temp_list);
        listChangedanwei.setAdapter(adapter);
        listChangedanwei.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                dw_id = Temp_list.get(position).getCompanyId();
                IsNeedData = 0;//此时不用再走onTextChanged
                etUserDanwei.setText(Temp_list.get(position).getCompName());
                liChangedanwei.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(etUserDanwei).isEmpty()) {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
            ivChangeDw.setVisibility(View.GONE);
        } else {
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
            ivChangeDw.setVisibility(View.VISIBLE);
        }
        if (IsNeedData != 0) {
            dw_id = "";
            liChangedanwei.setVisibility(View.GONE);
            if (charSequence.length() > 0) {
                getDWData();
            }
        } else {
            IsNeedData = 2;//恢复到需要走onTextChanged
        }
    }


    /**
     * 获取单位自动提示
     */
    private void getDWData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.DWList, Const.POST);
        mRequest.add("compName", etUserDanwei.getText().toString());//单位
        CallServer.getRequestInstance().add(ChangeDanWeiActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeDanWeiActivity.this, true, DanWeiList.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        DanWeiList model = ((DanWeiList) data);
                        Temp_list.clear();
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            liChangedanwei.setVisibility(View.GONE);
                        } else {
                            liChangedanwei.setVisibility(View.VISIBLE);
                            adapter.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                    }
                }, true, false);
    }

    /**
     * 修改个人资料
     */

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.UpdatePerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangeDanWeiActivity.this, "uid"));
        mRequest.add("companyId", dw_id);//单位
        CallServer.getRequestInstance().add(ChangeDanWeiActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeDanWeiActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangeDanWeiActivity.this, "danwei", etUserDanwei.getText().toString());
                        finish();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangeDanWeiActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

    /**
     * 单位自动补全列表
     */
    class DWListAdapter extends BaseAdapter {
        private List<DanWeiList.RowsBean> list;

        public DWListAdapter(List<DanWeiList.RowsBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_centertext, null);
            TextView tv_text_tilte = (TextView) v.findViewById(R.id.tv_textc_tilte);
            View view_ban = (View) v.findViewById(R.id.view_ctext_ban);
            View view_man = (View) v.findViewById(R.id.view_ctext_man);
            tv_text_tilte.setText(list.get(position).getCompName());
            if (position == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
            return v;
        }

    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.view_changebottom:
                liChangedanwei.setVisibility(View.GONE);
                break;
            case R.id.bt_dw_queding:
                if (TextUtils.isEmpty(etUserDanwei.getText().toString())) {
                    CommonUtil.showToask(ChangeDanWeiActivity.this, "请输入您的单位！");
                    return;
                }
                if (TextUtils.isEmpty(dw_id)) {
                    CommonUtil.showToask(ChangeDanWeiActivity.this, "请输入正确的单位！");
                    return;
                }
                getData();
                break;
            case R.id.iv_change_dw:
                etUserDanwei.setText("");
                break;
        }
    }
}
